﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

public partial class subjectUpLoad : System.Web.UI.Page
{
    private SqlCommand com;

    //声明页面的公共变量
    string userName;
    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化一个command对象
        SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["gConnectionString"]);
        com = new SqlCommand();
        com.Connection = conn;

        if(Session["userName"]==null)
        {
            Response.Redirect("../index.aspx");
        }
        else
        {
            userName=Session["userName"].ToString();
        }
    }
    protected void publish_Click(object sender, EventArgs e)
    {
        //发表一个课程
        insertRecord();
    }
    protected void up_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            upLoad();
        }
    }
    protected void upLoad()
    {
        ///'遍历File表单元素 
        HttpFileCollection files = HttpContext.Current.Request.Files;
        Response.Write(string.Format("<script language='javascript'>alert({0});</script>", files.Count));
        /// '状态信息 
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是：<hr color=red>");

        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字 
                HttpPostedFile postedFile = files[iFile];
                string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    strMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名：" + fileName + "<br>");
                    strMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr>");
                    ///'可根据扩展名字的不同保存到不同的文件夹 
                    ///注意：可能要修改你的文件夹的匿名写入权限。 
                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
                }
            }
        }

        catch (System.Exception Ex)
        {
            Response.Write(Ex.ToString());
        }
    }

    //对课程的增、查、删该
    private void insertRecord()
    {
            string strTitle = title.Text.Trim().ToString();
            string strContent = richeditor.text.ToString();
            int strImportant =Convert.ToInt16(important.Text.Trim());


            string sql = "insert into bbs(f_title,f_content,f_date,f_username1,f_ip,f_imp) values(@f_title,@f_content,@f_date,@f_userName1,@f_ip,@f_imp)";
            com.CommandType = CommandType.Text;
            com.CommandText = sql;
            SqlParameter strTitlePara=new SqlParameter("@f_title",SqlDbType.VarChar,200);
            strTitlePara.Value=strTitle;
            com.Parameters.Add(strTitlePara);
            SqlParameter strContentPara=new SqlParameter("@f_content",SqlDbType.NText);
            strContentPara.Value=strContent;
            com.Parameters.Add(strContentPara);
            SqlParameter strImportantPara = new SqlParameter("@f_imp", SqlDbType.Int);
            strContentPara.Value = strImportant;
            com.Parameters.Add(strImportantPara);
            com.Parameters.AddWithValue("@f_date", DateTime.Now);
            com.Parameters.AddWithValue("@f_userName1", userName);
            com.Parameters.AddWithValue("@f_ip", Request.UserHostAddress);
            int result = com.ExecuteNonQuery();

            if (result == 1)
            {
                Response.Write("<script language='javascript'>alert('添加成功！');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('添加失败！');</script>");
            }

        
       
    }

    private bool selectRecord()
    {
        if (true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private bool deleteRecord()
    {
        if (true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private bool updateRecord()
    {
        if (true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
